﻿<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加会员</title>
<meta name="renderer" content="webkit">	
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">	
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">	
<meta name="apple-mobile-web-app-status-bar-style" content="black">	
<meta name="apple-mobile-web-app-capable" content="yes">	
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/main.css">
<link rel="stylesheet" href="css/index.css">
<link rel="stylesheet" href="css/table.css">
</head>
<body>
	<div class="content-head">悦宝园会员管理系统</div>
	<div class="content-main">
		<div class="side-nav" ms-controller="navBar">
			<ul class="nav-item">
				<li><a href="main.html" class="nav-item-front nav-item-add">首页</a></li>
				<li ms-for="el in @navTitleArr">
					<a href="javascript:;" class="nav-item-front">{{el.par}}</a>
					<dl class="nav-item-child">
						<dd ms-for="($index, ex) in el.child"><a ms-attr='{href: el.href[$index]}'>{{ex}}</a></dd>
					</dl>
				</li>
			</ul>
		</div>
		<div class="content bg-gray"  ms-controller="main">
			<div class="tab-content">
				<div class="tab-item">
					<div class="sub-main">
						<p class="sub-title"><i>添加会员</i></p>
						<div class="import-area">
							<div class="import-block">
								<span class="import-name">宝宝姓名：</span>
								<div class="import-input"><input type="text" maxlength="20" ms-duplex="@name" id="name"></div>
							</div>
							<div class="import-block">
								<span class="import-name">宝宝小名：</span>
								<div class="import-input"><input type="text" maxlength="20" ms-duplex="@nickName" id="nickName"></div>
							</div>
							<div class="import-block">
								<span class="import-name">出生日期：</span>
								<div class="import-input"><input type="date" maxlength="20" ms-duplex="@birthday" id="birthday"></div>
							</div>
							<div class="import-block">
								<span class="import-name">家长姓名：</span>
								<div class="import-input"><input type="text" maxlength="20" ms-duplex="@paName" id="paName"></div>
							</div>
							<div class="import-block">
								<span class="import-name">电话：</span>
								<div class="import-input"><input type="tel" maxlength="11" ms-duplex="@tel" id="tel"></div>
							</div>
							<div class="import-block">
								<span class="import-name">报课日期：</span>
								<div class="import-input"><input type="date" maxlength="20" ms-duplex="@buyDay" id="buyDay"></div>
							</div>
							<div class="import-block">
								<span class="import-name">已报课包：</span>
								<div class="import-input"><input type="text" maxlength="3" style="width:50px;" ms-duplex-number="@classNum" id="classNum">课时</div>
							</div>
							<div class="import-block">
								<span class="import-name">已报价格：</span>
								<div class="import-input"><input type="text" maxlength="5" style="width:50px;" ms-duplex-number="@price" id="price">元</div>
							</div>
							<div class="import-block">
								<span class="import-name">合同有效期：</span>
								<div class="import-input"><input type="text" maxlength="2" style="width:50px;" ms-duplex-number="@period" id="period">月</div>
							</div>
							<div class="import-block">
								<span class="import-name">所属顾问：</span>
								<div class="import-input">
									<select ms-duplex="@cc">
										<option value="Maggie" selected="selected">Maggie</option>
										<option value="Cora">Cora</option>
										<option value="Anna">Anna</option>
										<option value="Lisa">Lisa</option>
										<option value="Mia">Mia</option>
										<option value="Katie">Katie</option>
										<option value="Ivy">Ivy</option>
										<option value="Lucas">Lucas</option>
										<option value="Amy">Amy</option>
									</select>
								</div>
							</div>
							<div class="import-block">
								<span class="import-name">会员来源：</span>
								<div class="import-input">
									<select ms-duplex="@source">
										<option value="1" selected="selected">毛单</option>
										<option value="2">采单</option>
										<option value="3">异业推荐</option>
										<option value="4">地推活动</option>
										<option value="5">转介绍</option>
										<option value="6">walk in</option>
										<option value="7">call in</option>
										<option value="8">web in</option>
										<option value="9">自采</option>
									</select>
								</div>
							</div>
							<div class="import-block">
								<span class="import-name"></span>
								<a href="javascript:;" class="import-btn" ms-on-click="@subForm">添加</a></div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<!-- <input type="file" id="file" value="批量添加会员信息"> -->
<script src="js/jq.js"></script>
<script src="js/avalon.js"></script>
<script src="js/main.js"></script>
<script src="js/xlsx.full.min.js"></script>
<script>
(function() {
	var obj = {};
	function getNowFormatDate() {
		var date = new Date();
		var seperator1 = "-";
		var year = date.getFullYear();
		var month = date.getMonth() + 1;
		var strDate = date.getDate();
		if (month >= 1 && month <= 9) {
			month = "0" + month;
		}
		if (strDate >= 0 && strDate <= 9) {
			strDate = "0" + strDate;
		}
		var currentdate = year + seperator1 + month + seperator1 + strDate;
		return currentdate;
	}
	var vm = avalon.define({
	    $id: "main",
	    name: "",
	    nickName: "",
	    birthday: "",
	    paName: "",
	    price: "",
	    period: "",
	    tel: "",
	    classNum: "",
	    cc: "Maggie",
	    source: "1",
	    buyDay: getNowFormatDate(),
	    subForm: function() {
	    	obj.name = $.trim(this.name);
	    	obj.nickName = $.trim(this.nickName);
	    	obj.birthday = $.trim(this.birthday);
	    	obj.paName = $.trim(this.faName);
	    	obj.price = $.trim(this.price);
	    	obj.period = $.trim(this.period);
	    	obj.tel = $.trim(this.tel);
	    	obj.classNum = {formal: $.trim(this.classNum), free: 0};
	    	obj.cc = $.trim(this.cc);
	    	obj.source = $.trim(this.source);
	    	obj.buyDay = $.trim(this.buyDay);
	    	obj.upClassInfo = {}; //{ name: [观察期提醒，升班提醒，处于哪个时期] }
	    	obj.buyInfo = [];

	    	var resObj = baseFunc.valide(obj),
	    		status = true;
	    		
	    	$(".valide").remove();

	    	for(var k in resObj) {
	    		if(resObj[k] === false) {
	    			$("#" + k).parent().append("<span class='valide' style='color:#e74c3c; padding:0 15px;'>输入不正确<span>");
	    			status = false;
	    		}
	    	}

	    	var diffDate = function(newDate, oldDate) { //{[], []}
	    		var year = newDate[0]; //表示需要查找的年份
				var month = newDate[1];//表示需要查找的月份
				var curMonthDays = new Date(year,month,0).getDate();//0表示3月的第0天，上月的最后一天,月份从0开始记数
				if(oldDate[2] > curMonthDays) {
					newDate[2] = curMonthDays;
				}

	    		return newDate[0] + "-" + newDate[1] + "-" + newDate[2];
	    	};

	    	var calcDate = function(arr, calcNum) {
	    		var oldY = parseInt(arr[0]),
	    			oldM = parseInt(arr[1]),
	    			oldD = parseInt(arr[2]),
	    			y = oldY,
	    			m = oldM,
	    			d = oldD,
	    			calcNum = parseInt(calcNum);

	    		if(calcNum == 12) {
	    			y += 1;
	    			return diffDate([y, m, d], [oldY, oldM, oldD]);
	    		}

	    		if(calcNum == 24) {
	    			y += 2;
	    			return diffDate([y, m, d], [oldY, oldM, oldD]);
	    		}

	    		if(calcNum < 12) {
	    			m = m + calcNum;
	    			if(m > 12) {
	    				y += 1;
	    				m -= 12;
	    			}
	    			return diffDate([y, m, d], [oldY, oldM, oldD]);
	    		}

	    		if(calcNum > 12) {
	    			y += 1;
	    			calcNum -= 12;
	    			m += calcNum;
	    			if(m > 12) {
	    				y += 1;
	    				m -= 12;
	    			}
	    			return diffDate([y, m, d], [oldY, oldM, oldD]);
	    		}
	    	};

	    	var inWhichTime = function(tipsDate, confirmDate) {
	    		//情况一刚好处于提醒日
	    		var day2 = new Date();
	    		day2.setTime(day2.getTime());
	    		var nowDate = day2.getFullYear() + "-" + (day2.getMonth()+1) + "-" + day2.getDate();
	    		var effDiff = baseFunc.dateDiff(tipsDate, confirmDate);
	    		var tipsDiff = baseFunc.dateDiff(tipsDate, nowDate);
	    		var confirmSplit = confirmDate.split("-");

		    	if(tipsDiff >= 0 && tipsDiff < effDiff) {
		    		return "inTips";
		    	}

	    		if(day2.getFullYear() == confirmSplit[0] && (day2.getMonth()+1) == confirmSplit[1] && day2.getDate() == confirmSplit[2]) {
	    			return "inConfirm";
	    		}
	    		return "free";
	    	};

	    	//预存升班信息
	    	//观察期提醒，  升班提醒推后一个月
			//bb：  5个月， 6个月
			//tod:  9个月， 10个月
			//run： 15个月，16个月
			//tows：23个月，24个月			
			//m1：  9个月， 10个月
			//m2：  15个月，16个月
			//m3：  23个月，24个月
			//ae1： 15个月，16个月
			//ae2： 23个月，24个月

	    	if(status) {
	    		var birthday = obj.birthday.split("-");
				obj.upClassInfo = {
					"bb": [calcDate(birthday, 5), calcDate(birthday, 6), inWhichTime(calcDate(birthday, 5), calcDate(birthday, 6))],
					"tod": [calcDate(birthday, 9), calcDate(birthday, 10), inWhichTime(calcDate(birthday, 9), calcDate(birthday, 10))],
					"run": [calcDate(birthday, 15), calcDate(birthday, 16), inWhichTime(calcDate(birthday, 15), calcDate(birthday, 16))],
					"twos": [calcDate(birthday, 23), calcDate(birthday, 24), inWhichTime(calcDate(birthday, 23), calcDate(birthday, 24))],
					"m1": [calcDate(birthday, 9), calcDate(birthday, 10), inWhichTime(calcDate(birthday, 9), calcDate(birthday, 10))],
					"m2": [calcDate(birthday, 15), calcDate(birthday, 16), inWhichTime(calcDate(birthday, 15), calcDate(birthday, 16))],
					"m3": [calcDate(birthday, 23), calcDate(birthday, 24), inWhichTime(calcDate(birthday, 23), calcDate(birthday, 24))],
					"ae1": [calcDate(birthday, 15), calcDate(birthday, 16), inWhichTime(calcDate(birthday, 15), calcDate(birthday, 16))],
					"ae2": [calcDate(birthday, 23), calcDate(birthday, 24), inWhichTime(calcDate(birthday, 23), calcDate(birthday, 24))]
				};
				
				obj.buyInfo.push({date: obj.buyDay, num: parseInt(obj.classNum.formal), price: parseInt(obj.price)});

	    		baseFunc.setStorageData("member", obj);
	    		baseFunc.locationSkip("allMember.html");
	    	}
	    }
	})

/***********************************************************************************/
	/*
        FileReader共有4种读取方法：
        1.readAsArrayBuffer(file)：将文件读取为ArrayBuffer。
        2.readAsBinaryString(file)：将文件读取为二进制字符串
        3.readAsDataURL(file)：将文件读取为Data URL
        4.readAsText(file, [encoding])：将文件读取为文本，encoding缺省值为'UTF-8'
                     */
        var wb;//读取完成的数据
        var rABS = false; //是否将文件读取为二进制字符串


        function fixdata(data) { //文件流转BinaryString
            var o = "",
                l = 0,
                w = 10240;
            for(; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)));
            o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
            return o;
        }

        var diffDate = function(newDate, oldDate) { //{[], []}
    		var year = newDate[0]; //表示需要查找的年份
			var month = newDate[1];//表示需要查找的月份
			var curMonthDays = new Date(year,month,0).getDate();//0表示3月的第0天，上月的最后一天,月份从0开始记数
			if(oldDate[2] > curMonthDays) {
				newDate[2] = curMonthDays;
			}

    		return newDate[0] + "-" + newDate[1] + "-" + newDate[2];
    	};

    	var calcDate = function(arr, calcNum) {
    		var oldY = parseInt(arr[0]),
    			oldM = parseInt(arr[1]),
    			oldD = parseInt(arr[2]),
    			y = oldY,
    			m = oldM,
    			d = oldD,
    			calcNum = parseInt(calcNum);

    		if(calcNum == 12) {
    			y += 1;
    			return diffDate([y, m, d], [oldY, oldM, oldD]);
    		}

    		if(calcNum == 24) {
    			y += 2;
    			return diffDate([y, m, d], [oldY, oldM, oldD]);
    		}

    		if(calcNum < 12) {
    			m = m + calcNum;
    			if(m > 12) {
    				y += 1;
    				m -= 12;
    			}
    			return diffDate([y, m, d], [oldY, oldM, oldD]);
    		}

    		if(calcNum > 12) {
    			y += 1;
    			calcNum -= 12;
    			m += calcNum;
    			if(m > 12) {
    				y += 1;
    				m -= 12;
    			}
    			return diffDate([y, m, d], [oldY, oldM, oldD]);
    		}
    	};

    	var inWhichTime = function(tipsDate, confirmDate) {
    		//情况一刚好处于提醒日
    		var day2 = new Date();
    		day2.setTime(day2.getTime());
    		var nowDate = day2.getFullYear() + "-" + (day2.getMonth()+1) + "-" + day2.getDate();
    		var effDiff = baseFunc.dateDiff(tipsDate, confirmDate);
    		var tipsDiff = baseFunc.dateDiff(tipsDate, nowDate);
    		var confirmSplit = confirmDate.split("-");

	    	if(tipsDiff >= 0 && tipsDiff < effDiff) {
	    		return "inTips";
	    	}

    		if(day2.getFullYear() == confirmSplit[0] && (day2.getMonth()+1) == confirmSplit[1] && day2.getDate() == confirmSplit[2]) {
    			return "inConfirm";
    		}
    		return "free";
    	};

    	function faDate(date) {
    		if(parseInt(date) <= 9) {
    			return "0" + date;
    		} 
    		return date;
    	}

		function formatDate(serial) {
		   var utc_days  = Math.floor(serial - 25569);
		   var utc_value = utc_days * 86400;                                        
		   var date_info = new Date(utc_value * 1000);
		   var fractional_day = serial - Math.floor(serial) + 0.0000001;
		   var total_seconds = Math.floor(86400 * fractional_day);
		   var seconds = total_seconds % 60;
		   total_seconds -= seconds;
		   var hours = Math.floor(total_seconds / (60 * 60));
		   var minutes = Math.floor(total_seconds / 60) % 60;
		   var s = new Date(date_info.getFullYear(), date_info.getMonth(), date_info.getDate(), hours, minutes, seconds);
		   return s.getFullYear() + "-" + faDate(s.getMonth()+1) + "-" + faDate(s.getDate());
		}

		function addUpClass(birthday) {
			birthday = birthday.split("-");
			if (birthday) {
				return {
					"bb": [calcDate(birthday, 5), calcDate(birthday, 6), inWhichTime(calcDate(birthday, 5), calcDate(birthday, 6))],
					"tod": [calcDate(birthday, 9), calcDate(birthday, 10), inWhichTime(calcDate(birthday, 9), calcDate(birthday, 10))],
					"run": [calcDate(birthday, 15), calcDate(birthday, 16), inWhichTime(calcDate(birthday, 15), calcDate(birthday, 16))],
					"twos": [calcDate(birthday, 23), calcDate(birthday, 24), inWhichTime(calcDate(birthday, 23), calcDate(birthday, 24))],
					"m1": [calcDate(birthday, 9), calcDate(birthday, 10), inWhichTime(calcDate(birthday, 9), calcDate(birthday, 10))],
					"m2": [calcDate(birthday, 15), calcDate(birthday, 16), inWhichTime(calcDate(birthday, 15), calcDate(birthday, 16))],
					"m3": [calcDate(birthday, 23), calcDate(birthday, 24), inWhichTime(calcDate(birthday, 23), calcDate(birthday, 24))],
					"ae1": [calcDate(birthday, 15), calcDate(birthday, 16), inWhichTime(calcDate(birthday, 15), calcDate(birthday, 16))],
					"ae2": [calcDate(birthday, 23), calcDate(birthday, 24), inWhichTime(calcDate(birthday, 23), calcDate(birthday, 24))]
				};
			}
			return {};
		}


        $("#file").change(function(){
            if(!this.files) {
                return;
            }
            var f = this.files[0];
            var reader = new FileReader();
            // // 读取本地excel文件
		    reader.onload = function(e) {
		        var data = e.target.result;
		        var workbook = XLSX.read(data, {type: 'binary'});
		        var sheetNames = workbook.SheetNames; // 工作表名称集合
		        var arr = {};
		        var allArr = [];
		        var p = 0;
		        arr.classNum = {};
			    sheetNames.forEach(name => {
			        var worksheet = workbook.Sheets[name]; // 只能通过工作表名称来获取指定工作表
			        
			        for(var key in worksheet) {
			            // v是读取单元格的原始值
			            if(key[0] !== '!') {
			            	p++;
			            	if (p == 1) { //姓名
			            		arr.name = key[0] === '!' ? worksheet[key] : worksheet[key].v;
			            	}
			            	if(p == 2) { //购买信息
			            		 arr.buyInfo = key[0] === '!' ? [{date: "", num: parseInt(worksheet[key]), price: ""}] : [{date: "", num: parseInt(worksheet[key].v), price: ""}];
			            	}
			            	if (p == 3) { //赠课
			            		arr.classNum = key[0] === '!' ? {formal: 0, free: parseInt(worksheet[key])} : {formal: 0, free: parseInt(worksheet[key].v)};
			            		// arr.buyInfo = key[0] === '!' ? [{date: "", num: parseInt(worksheet[key]), price: ""}] : [{date: "", num: parseInt(worksheet[key].v), price: ""}];
			            	}
			            	if (p == 6) {//剩余课
			            		arr.classNum.formal = key[0] === '!' ? worksheet[key] : worksheet[key].v;
			            		// arr.classNum = key[0] === '!' ? {formal: parseInt(worksheet[key]), free: arr.classNum.free || 0} : {formal: parseInt(worksheet[key]).v, free: arr.classNum.free || 0};
			            		// arr.buyInfo = key[0] === '!' ? [{date: "", num: parseInt(worksheet[key]), price: ""}] : [{date: "", num: parseInt(worksheet[key].v), price: ""}];
			            	}
			            	if (p == 8) { //小名
			            		arr.nickName = key[0] === '!' ? worksheet[key] : worksheet[key].v;
			            	}
			            	if(p == 10) { //出生年月
			            		arr.birthday = key[0] === '!' ? formatDate(worksheet[key]) : formatDate(worksheet[key].v);
			            		arr.upClassInfo = key[0] === '!' ? addUpClass(formatDate(worksheet[key])) : addUpClass(formatDate(worksheet[key].v));
			            	}
			            	if(p == 12) { //父母姓名
			            		arr.paName = key[0] === '!' ? worksheet[key] : worksheet[key].v;
			            	}
			            	if(p == 13) { //电话
			            		arr.tel = key[0] === '!' ? worksheet[key] : worksheet[key].v;
			            	}
			            	if (p == 14) {
			            		arr.buyInfo[0].date = key[0] === '!' ? worksheet[key] : worksheet[key].v;
			            	}
			            	if(p == 15) { //合同到期
			            		arr.contractOff = key[0] === '!' ? [worksheet[key]] : [worksheet[key].v];
			            	}
			            	
			            	if(p > 14) {
								allArr.push(arr);
								baseFunc.setStorageData("member", arr);
								arr = {};
								p = 0;
			            	}

			            }
			        }
			    });
		    };
		    reader.readAsBinaryString(f);
        })
})();
</script>
</body>
</html>